package demo1;

/**
 * Created with IntelliJ IDEA.
 * User: 12629
 * Date: 2022/2/25
 * Time: 20:03
 * Description:
 */
class Money implements Cloneable{
    public double money=10.2;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class Person implements Cloneable{
    public int id;
    public Money m = new Money();

    public Person(int id) {
        this.id = id;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person personCopy = (Person)super.clone();
        personCopy.m = (Money)this.m.clone();
        return personCopy;
    }
}

public class TestDemo3 {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person person = new Person(110);
        System.out.println(person);

        Person person2 = (Person)person.clone();
        System.out.println(person2);

        System.out.println("修改一下money：");
        person2.m.money = 99.9;
        System.out.println(person.m.money);//99.9
        System.out.println(person2.m.money);//99.9
    }
}
